package com.sykj.exception;

import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;

import org.apache.shiro.authc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * @program: BeautyClub
 * @description: 自定义全局异常类
 * @author: qiaoli
 * @create: 2020-07-15 09:33
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    public ResultBody bizExceptionHandler(HttpServletRequest req, BizException e){
        logger.error("发生业务异常！原因是：{}",e.getErrorMsg());
        return new ResultBody(e.getErrorCode(),e.getErrorMsg());
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e){
        logger.error("发生空指针异常！原因是:",e);
        return new ResultBody(CommonEnum.BODY_NOT_MATCH);
    }


    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =ClassNotFoundException.class)
    public ResultBody exceptionHandler(HttpServletRequest req, IncorrectCredentialsException e){
        logger.error("未知异常！原因是:",e);
        return new ResultBody(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * shiro 密码错误异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = IncorrectCredentialsException.class)
    public ResultBody incorrectCredentialsException(HttpServletRequest req, IncorrectCredentialsException e){
        return new ResultBody(CommonEnum.SHIRO_MMCW);
    }

    /**
     * shiro 帐号被禁用异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = DisabledAccountException.class)
    public ResultBody DisabledAccountException(HttpServletRequest req, DisabledAccountException e){
        return new ResultBody(CommonEnum.SHIRO_ZHJY);
    }

    /**
     * shiro 帐号被锁定异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = LockedAccountException.class)
    public ResultBody LockedAccountException(HttpServletRequest req, LockedAccountException e){
        return new ResultBody(CommonEnum.SHIRO_ZHSD);
    }
    /**
     * shiro 登录失败次数过多异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = ExcessiveAttemptsException.class)
    public ResultBody ExcessiveAttemptsException(HttpServletRequest req, ExcessiveAttemptsException e){
        return new ResultBody(CommonEnum.SHIRO_DLCSGD);
    }


    /**
     * shiro 登录凭证过期异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = AuthenticationException.class)
    public ResultBody AuthenticationException(HttpServletRequest req, AuthenticationException e){
        return new ResultBody(CommonEnum.SHIRO_DLPZGQ);
    }
 /**
  * 功能描述:redis 远程主机强迫关闭连接
  * @Param:
  * @Return:
  * @Author: Feiwo Sicun
  * @Date: 2020/7/29 23:41
  */
 @ExceptionHandler(value = RedisSystemException.class)
 public ResultBody  RedisIOErrorException(HttpServletRequest req, AuthenticationException e){
     return  new ResultBody(CommonEnum.RedisSystemExcption);
 }
}